<?php
/*
Plugin Name: Twictures Search
Plugin URI:  http://code.google.com/p/twictures/
Description: Twictures search widget.
Version: 1.0
Author: Sergey Gusev
Author URI: http://beastman.me
License: MIT
 */


define('TWICTURES_SEARCH_VERSION', '1.0');
add_action( 'wp_print_styles', 'twictures_search_styles' );
add_action( 'wp_print_scripts', 'twictures_search_scripts' );
add_action('widgets_init', create_function('', 'return register_widget("WpTwicturesSearchWidget");'));
add_action('wp_ajax_twictures_search_post', 'twictures_search_ajax_post');
add_action('wp_ajax_nopriv_twictures_search_post', 'twictures_search_ajax_post');
function twictures_search_styles() {
    wp_enqueue_style( 'wp-twictures-search-css', twictures_search_plugin_url ( 'views/styles.css' ), array(), TWICTURES_SEARCH_VERSION, 'all' );
}
function twictures_search_scripts() {
    wp_enqueue_script( 'wp-twictures-search-js', twictures_search_plugin_url( 'views/scripts.js' ), array( 'jquery' ), TWICTURES_SEARCH_VERSION, false );
?>
<script type="text/javascript">
    twictures_search_ajax_url='<?php echo admin_url('admin-ajax.php');?>';
</script>
<?
}
function twictures_search_plugin_url( $path = '' ) {
	return plugins_url( $path, plugin_basename( __FILE__ ));
}
function twictures_search_ajax_post(){
    $widget=new WpTwicturesSearchWidget();
    $widget->TwicturesSearchWidget->showResults($_POST); 
    die();
}



class WpTwicturesSearchWidget extends WP_Widget {

    public $TwicturesSearchWidget = ''; //Base Widget class

    //Constructor
    public function WpTwicturesSearchWidget(){
        parent::WP_Widget(false, $name = 'Twictures Search Widget');
        require_once(dirname(__FILE__) . '/class.TwicturesSearchWidget.php');
        $script_dir = dirname(realpath($_SERVER['SCRIPT_FILENAME']));
        $script_dir = str_replace('\\','/',$script_dir);
        $request_path = dirname($_SERVER['SCRIPT_NAME']);
        $document_root = substr($script_dir,0,strrpos($script_dir, $request_path));
        $this->TwicturesSearchWidget = new TwicturesSearchWidget($document_root); 
    }
    
    public function widget($args, $instance) {
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        echo $before_widget;
        if ( $title ) echo $before_title . $title . $after_title;
        if( $_POST['action'] == "twictures_search_post" ) {
            $this->TwicturesSearchWidget->showResults($_POST);
        }
        $this->TwicturesSearchWidget->showWidget('');
        echo $after_widget;
    }
    
    
    public function update($new_instance, $old_instance) {				
        return $new_instance;
    }
    
    
    public function form($instance) {				
        $title = esc_attr($instance['title']);
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php 
    }
}
